<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="88px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="terminalName">
        <el-input
          v-model="queryParams.terminalName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="terminalNo">
        <el-input
          v-model="queryParams.terminalNo"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户级别" prop="userType">
        <el-select v-model="queryParams.userType" clearable>
          <el-option
            v-for="item in userTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属代理商" prop="agentId">
        <el-select v-model="queryParams.agentId" clearable>
          <el-option
            v-for="item in agentList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="payMethod">
        <el-select v-model="queryParams.payMethod" clearable>
          <el-option
            v-for="item in orderTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单时间">
         <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>

    </el-form>
    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%;"
    >
      <el-table-column label="序号" type="index" >
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单号"  prop="orderNo" />
      <el-table-column label="设备名称"  prop="terminalName" />
      <el-table-column label="设备编号"  prop="terminalNo" />
      <el-table-column label="手机号"  prop="phone" />
      <el-table-column label="用户级别">
        <template slot-scope="scope">
          <span v-show="scope.row.userType == 0">非会员</span>
          <span v-show="scope.row.userType == 1">会员</span>
        </template>
      </el-table-column>
      <el-table-column label="用户余额" prop="balance" />
      <el-table-column label="所属代理商"  prop="agentName" />
      <el-table-column label="所属门店">
        <template slot-scope="scope">
          {{scope.row.storeName ? scope.row.storeName : '无'}}
        </template>
      </el-table-column>
      <el-table-column label="所属车队">
        <template slot-scope="scope">
          {{scope.row.fleetName ? scope.row.fleetName : '无'}}
        </template>
      </el-table-column>
      <el-table-column label="订单类型" prop="payMethod" >
        <template slot-scope="scope">
          <span v-show="scope.row.payMethod == 0">充值</span>
          <span v-show="scope.row.payMethod == 1">加注</span>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" >
        <template slot-scope="scope">
          {{scope.row.amount ? Number(scope.row.amount).toFixed(2) : '0'}}
        </template>
      </el-table-column>
      <el-table-column label="退款金额">
        <template slot-scope="scope">
          {{scope.row.refundAmount ? Number(scope.row.refundAmount).toFixed(2) : '0'}}
        </template>
      </el-table-column>
      <el-table-column label="返利金额" >
        <template slot-scope="scope">
          {{scope.row.rebateAmount ? Number(scope.row.rebateAmount).toFixed(2) : '0'}}
        </template>
      </el-table-column>
      <el-table-column label="最终金额">
        <template slot-scope="scope">
          {{scope.row.finalAmount ? Number(scope.row.finalAmount).toFixed(2) : '0'}}
        </template>
      </el-table-column>
      <el-table-column label="订单时间" prop="createdTime" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { getOrderList,getAgentList } from "@/api/common";

export default {
  name: "Online",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params:{}
      },
      agentList:[],
      userTypeList:[
        {value:'1',label:'会员'},
        {value:'0',label:'非会员'},
      ],
      orderTypeList:[
        {value:'1',label:'加注'},
        {value:'0',label:'充值'},
      ],
      dateRange:[]
    };
  },
  created() {
    this.getAgent()
    this.getList();
  },
  watch:{
    dateRange(val){
      console.log(val)
      if(val){
        this.queryParams.params.startDate = val[0]
        this.queryParams.params.endDate = val[1]
      }else{
        this.queryParams.params = {}
      }
    }
  },
  methods: {
    getAgent(){
      let req = {
        userType:'01'
      }
      getAgentList(req).then(res=>{
        if(res.code == 200){
          this.agentList = res.rows.map(e=>{
            return{
              label:e.nickName,
              value:e.userId
            }
          })
        }
      })
    },
    getList() {
      this.loading = true;
      getOrderList(this.queryParams).then(response => {
        this.list = response.rows.map(item=>{
          const { amount,refundAmount,rebateAmount } = item
          let finalAmount = (amount && refundAmount && rebateAmount) ? (amount - refundAmount - rebateAmount) : null
          return {
            ...item,
            percent:item.currentCapacity / item.capacity,
            finalAmount
          }
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams =  {
        pageNum: 1,
        pageSize: 10,
        params:{}
      }
      this.handleQuery();
    }
  }
};
</script>

<style lang="scss" scoped>
.warnStatus{
  display: flex;
  justify-content: center;
}
.circle{
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
.terminalContent{
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  .terminalItem{
    width: 50%;
  }
}
.dealContent{
  display: flex;
  align-items: center;
  height: 40px;
}
</style>

